//
//  TLShareCenter.m
//  FunnyVideo
//
//  Created by 李鑫 on 16/1/3.
//  Copyright © 2016年 NewEggTech. All rights reserved.
//

#import "TLShareCenter.h"
#import "TLAlertHelper.h"
#import <AVOSCloud/AVOSCloud.h>
#import <ShareSDK/ShareSDK.h>

@implementation TLShareCenter

+ (TLShareCenter *)center{
    static dispatch_once_t onceToken;
    static TLShareCenter *center;
    dispatch_once(&onceToken, ^{
        center = [[TLShareCenter alloc] init];
    });
    
    return center;
}

- (id)init{
    self = [super init];
    
    if(self)
    {
        self.wxAuthed = [ShareSDK hasAuthorized:SSDKPlatformTypeWechat];
    }
    
    return self;
}

+ (BOOL)isWxAuthed{
    return [ShareSDK hasAuthorized:SSDKPlatformTypeWechat];
}
- (void)weixinLogin:(TLSSOLoginCallBack)callBack{
    [ShareSDK authorize:SSDKPlatformTypeWechat settings:nil onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        callBack(state,user,error);
        
    }];
}
- (void)shareWXMoment:(NSString *)url image:(UIImage *)image text:(NSString *)text callBack:(TLWXMomentShareCallBack)callBack{
//    if([ShareSDK hasAuthorized:SSDKPlatformTypeWechat])
//    {
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:text
                                         images:image
                                            url:[NSURL URLWithString:url]
                                          title:@"疯狂一刻"
                                           type:SSDKContentTypeWebPage];
        
        //进行分享
        [ShareSDK share:SSDKPlatformSubTypeWechatTimeline
             parameters:shareParams
         onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
             
             switch (state) {
                 case SSDKResponseStateSuccess:
                 {
                     [TLAlertHelper showSuccessHUDWithText:@"分享成功"];
                     break;
                 }
                 case SSDKResponseStateFail:
                 {
                                          [TLAlertHelper showErrorHUDWithText:@"分享失败"];
                     break;
                 }
                 case SSDKResponseStateCancel:
                 {
                     [TLAlertHelper showErrorHUDWithText:@"分享取消"];
                     break;
                 }
                 default:
                     break;
             }
             
         }];
//    }
//    else
//    {
//        [self weixinLogin:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
//            if(!error)
//            {[self shareWXMoment:url callBack:^(BOOL succ, NSDictionary *callBack, NSError *err) {
//                
//            }];}
//            else
//            {
//                [TLAlertHelper showErrorHUDWithText:([error userInfo][@"error_message"])];
//            }
//        }];
//    }
}

@end
